{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 请添加自己的实验代码，点击运行查看结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unindent does not match any outer indentation level (<tokenize>, line 22)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<tokenize>\"\u001b[0;36m, line \u001b[0;32m22\u001b[0m\n\u001b[0;31m    html_data = response.json()['data']\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n"
     ]
    }
   ],
   "source": [
    "import requests  \n",
    "import parsel\n",
    "import csv\n",
    "f = open('天气.csv', mode='a', encoding='utf-8', newline='')\n",
    "csv_writer = csv.writer(f)\n",
    "csv_writer.writerow(['日期', '最高温度', '最低温度', '天气', '风向', '城市'])\n",
    "city_list = [54511, 58362, 59287, 59493]\n",
    "for city in city_list:\n",
    "    city_name = ''\n",
    "    if city == 54511:\n",
    "        city_name = '北京'\n",
    "    elif city == 58362:\n",
    "        city_name = '上海'\n",
    "    elif city == 59287:\n",
    "        city_name = '广州'\n",
    "    elif city == 59493:\n",
    "        city_name = '深圳'\n",
    "    for year in range(2013, 2023):\n",
    "        for month in range(1, 13):\n",
    "                    response = requests.get(url=url)\n",
    "\n",
    "            html_data = response.json()['data']\n",
    "select = parsel.Selector(html_data)\n",
    "            trs = select.css('.history-table tr')   \n",
    "            for tr in trs[1:]:                     \n",
    "                tds = tr.css('td::text').getall()   \n",
    "                tds.append(city_name)               \n",
    "                print(tds)\n",
    "                csv_writer.writerow(tds)\n",
    "import pandas as pd\n",
    "import datetime\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import *\n",
    "from pyecharts.commons.utils import JsCode\n",
    "data = pd.read_csv('天气.csv')\n",
    "data\n",
    "\n",
    "data[['日期','星期']] = data['日期'].str.split(' ',expand=True,n=1)\n",
    "\n",
    "data\n",
    "data[['最高温度','最低温度']] = data[['最高温度','最低温度']].apply(lambda x: x.str.replace('°','').replace('', '0'))\n",
    "data.head()\n",
    "data.loc[data['天气'].str.contains('雪'),'下雪吗']='是'\n",
    "data.fillna('否',inplace=True)\n",
    "data['日期'] = pd.to_datetime(data['日期'])\n",
    "data[['最高温度','最低温度']] = data[['最高温度','最低温度']].astype('int')\n",
    "data['年份'] = data['日期'].dt.year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
